﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Medicalsystem.MCP.FormDesigner.Common;
using Medicalsystem.MCP.FormDesigner.Define;
using Medicalsystem.MCP.FormDesigner.Interface;
using Medicalsystem.MCP.FormDesigner.Property;

namespace Medicalsystem.MCP.FormDesigner
{
    /// <summary>
    /// 控件管理者--管理控件事务
    /// </summary>
    public class CtrlMngDesign : CtrlMng
    {
        private System.Windows.Forms.Control _owner;

        private bool _BackLock = false;
        private bool _focused = false;

        private ContainerHandler _parentContainerHandler;    //控件父容器
        private ModelDesign _modelDesign;

        private UIMoveKnob _UIMKnob = null;            //该值可空 继承 ICtrlContainer 接口的控件使用
        private MoveHandler _moveHandler = null;       //控件的移动管理

        private List<UIContainerHandler> _containerHandlerList = null;
        private UIContainerHandler _childContainerFocused;

        /// <summary>
        /// 控件管理设计时
        /// </summary>
        /// <param name="owner">控件主体</param>
        /// <param name="parentContainer">控件容器</param>
        /// <param name="modelDesign">模板控制者</param>
        public CtrlMngDesign(ICtrlBase owner, ContainerHandler parentContainer, ModelDesign modelDesign)
            : base(owner)
        {
            this._owner = (System.Windows.Forms.Control)owner;
            this._modelDesign = modelDesign;
            this._parentContainerHandler = parentContainer;

            this._containerHandlerList = new List<UIContainerHandler>();

            if (_owner is ICtrlContainer)
            {
                this._UIMKnob = new UIMoveKnob(this);

                this._moveHandler = new MoveHandler(this, this._UIMKnob.UIMoveDot);

                foreach (Control C in ((ICtrlContainer)_owner).ControlContainers)
                {
                    UIContainerHandler ch = new UIContainerHandler(C, this, this._modelDesign);
                    this._containerHandlerList.Add(ch);
                }

                _childContainerFocused = _containerHandlerList[0];
            }
            else
            {
                this._moveHandler = new MoveHandler(this, _owner);
            }

            //键盘事件（获取键盘事件-将信息传递到设计器）
            this._owner.KeyDown += new System.Windows.Forms.KeyEventHandler(Owner_KeyDown);
            this._owner.KeyUp += new System.Windows.Forms.KeyEventHandler(Owner_KeyUp);
            ((ICtrlBase)_owner).PropertyObject.SetPropertyBatchEvent += new GetCtrlProperty.PropertyHandler(Owner_SetPropertyBatchEvent); //new GetCtrlUIProperty.SetPropertyBatchHandler(Owner_SetPropertyBatchEvent);

            //属性更改
            this._owner.LocationChanged += new EventHandler(Owner_PropertyChanged);
            this._owner.SizeChanged += new EventHandler(Owner_PropertyChanged);
        }

        /// <summary>
        /// 父级控件管理者
        /// </summary>
        public CtrlMngDesign ParentCtrlMngDesign
        {
            get { return _parentContainerHandler.ParentControlMngDesign; }
        }

        /// <summary>
        /// 控件容器
        /// </summary>
        public ContainerHandler ParentContainer
        {
            get { return _parentContainerHandler; }
        }

        /// <summary>
        /// 子容器
        /// </summary>
        public List<UIContainerHandler> ContainerHandlerList
        {
            get { return _containerHandlerList; }
        }

        /// <summary>
        /// 焦点子容器
        /// </summary>
        public UIContainerHandler ChildContainerFocused
        {
            get { return _childContainerFocused; }
            set { _childContainerFocused = value; }
        }

        /// <summary>
        /// 属于的模板管理者
        /// </summary>
        public ModelDesign ModelDesign
        {
            get { return _modelDesign; }
        }

        /// <summary>
        /// 关联的控件
        /// </summary>
        public System.Windows.Forms.Control Owner
        {
            get { return _owner; }
        }

        /// <summary>
        /// 控件关联的捏手相关-可能为 null
        /// </summary>
        public UIMoveKnob UIMKnob
        {
            get { return _UIMKnob; }
        }

        /// <summary>
        /// 锁定到底层
        /// </summary>
        public bool BackLock
        {
            get { return _BackLock; }
            set { _BackLock = value; }
        }

        public bool Focused
        {
            get { return _focused; }
            set
            {
                if (value)
                {
                    _parentContainerHandler.SetControlKnob(this);
                }
                else
                {
                    _parentContainerHandler.SetControlKnob(null);
                }

                _focused = value;
            }
        }

        public void BringToFront()
        {
            this.Owner.BringToFront();

            this.ParentContainer.KnobBringToFront();

            if (this._UIMKnob != null)
            {
                _UIMKnob.BringToFront();
            }

            //重新设置列表顺序
            this.ModelDesign.Controls.Ctrls.Remove(this);
            this.ModelDesign.Controls.Ctrls.Insert(0, this);

            this.ParentContainer.Controls.Remove(this);
            this.ParentContainer.Controls.Insert(0, this);
        }

        public void SendToBack()
        {
            this.Owner.SendToBack();

            //重新设置列表顺序
            this.ModelDesign.Controls.Ctrls.Remove(this);
            this.ModelDesign.Controls.Ctrls.Add(this);

            this.ParentContainer.Controls.Remove(this);
            this.ParentContainer.Controls.Add(this);
        }

        /// <summary>
        /// 注销
        /// </summary>
        public void Dispose()
        {
            if (this._UIMKnob != null)
            {
                this._UIMKnob.Dispose();
            }

            this._owner.Dispose();
        }

        #region 私有方法

        /// <summary>
        /// 处理键盘事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Owner_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            try
            {
                switch (e.KeyCode)
                {
                    case Keys.Left:
                        this._moveHandler.ControlMove(-1, 0);
                        this._parentContainerHandler.Container.Refresh();
                        break;

                    case Keys.Right:
                        this._moveHandler.ControlMove(1, 0);
                        this._parentContainerHandler.Container.Refresh();
                        break;

                    case Keys.Up:
                        this._moveHandler.ControlMove(0, -1);
                        this._parentContainerHandler.Container.Refresh();
                        break;

                    case Keys.Down:
                        this._moveHandler.ControlMove(0, 1);
                        this._parentContainerHandler.Container.Refresh();
                        break;

                    case Keys.A:
                    case Keys.C:
                    case Keys.V:
                    case Keys.X:
                    case Keys.Z:
                    case Keys.F4:
                    case Keys.F5:
                    case Keys.Delete:
                        SingleList.Instance.OnMessageEvent(e, MessageType.KeyEvent);
                        break;

                    case Keys.ControlKey:
                        this._modelDesign.CtrlKeyDown = true;
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                TryCustomException.TransactException(ex);
            }
        }

        /// <summary>
        /// 处理键盘事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Owner_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            try
            {
                if (e.KeyCode == System.Windows.Forms.Keys.ControlKey)
                {
                    this._modelDesign.CtrlKeyDown = false;
                }
            }
            catch (Exception ex)
            {
                TryCustomException.TransactException(ex);
            }
        }

        private void Owner_PropertyChanged(object sender, EventArgs e)
        {
            /*属性改变触发时间：
             * 1、通过属性编辑器改变控件属性
             * 2、通过移动控制改变控件属性
             */

            if (this._UIMKnob != null)
            {
                //目前 当属性显示框 改变控件属性时执行

                this._UIMKnob.SetUIMoveDotPosition();
            }

            if (this._focused && this._modelDesign.PropertyChangeAvail)
            {
                this.ParentContainer.SetControlKnob(this);
            }
        }

        /// <summary>
        /// 批量设置控件属性
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="propertyName"></param>
        /// <param name="value"></param>
        private void Owner_SetPropertyBatchEvent(object sender, string propertyName, object value)
        {
            try
            {
                foreach (CtrlMngDesign CtrlMngDesign in this._modelDesign.SelectList)
                {
                    //只设置同一容器内的控件属性 -
                    if ((_modelDesign.CtrlMngDesignFocused != null) && (_modelDesign.CtrlMngDesignFocused.Owner.Parent == CtrlMngDesign.Owner.Parent))
                    {
                        (CtrlMngDesign.Owner as ICtrlBase).PropertyObject.SetProperty(propertyName, value);
                    }
                }
            }
            catch (Exception ex)
            {
                TryCustomException.TransactException(ex);
            }
        }

        #endregion 私有方法

        public ContainerHandler GetContainerHandler(string name)
        {
            if (_containerHandlerList != null && _containerHandlerList.Count > 0)
            {
                if (_containerHandlerList.Count == 1)
                {
                    return _containerHandlerList[0];
                }
                else
                {
                    foreach (ContainerHandler ch in _containerHandlerList)
                    {
                        if (ch.Container.Name.Trim().ToLower().Equals(name.Trim().ToLower()))
                        {
                            return ch;
                        }
                    }
                }//
            }

            return null;
        }
    }
}